package com.stylefeng.guns.modular.system.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.modular.small.util.ResponseResultDTO;
import com.stylefeng.guns.modular.system.dao.ConsumerCollectionMapper;
import com.stylefeng.guns.modular.system.model.ArtificerMeans;
import com.stylefeng.guns.modular.system.model.ConsumerCollection;
import com.stylefeng.guns.modular.system.service.IConsumerCollectionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 用户收藏 服务实现类
 * </p>
 *
 * @author stylefeng
 * @since 2019-09-09
 */
@Service
@Slf4j
public class ConsumerCollectionServiceImpl extends ServiceImpl<ConsumerCollectionMapper, ConsumerCollection> implements IConsumerCollectionService {

    @Resource
    private ConsumerCollectionMapper consumerCollectionMapper;


    @Override
    public ResponseResultDTO searchByConsumerId(Integer consumerId) {
        ResponseResultDTO resultDTO = new ResponseResultDTO();
        try {
            List<ArtificerMeans> list = consumerCollectionMapper.selectListByConsumerId(consumerId);
            resultDTO.setData(list);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("系统异常，异常信息为：{}", e.getMessage());
            resultDTO.setMsg("系统异常");
            resultDTO.setStatus("-1");
        }
        return resultDTO;
    }

    @Override
    public ResponseResultDTO searchByConsumerIdArtificerId(Integer consumerId, Integer artificerId) {
        ResponseResultDTO resultDTO = new ResponseResultDTO();
        try {
            if(consumerId==null||artificerId==null){
                resultDTO.setMsg("PARAM IS NULL");
                resultDTO.setStatus("-1");
                return resultDTO;
            }
            List<ArtificerMeans> list = consumerCollectionMapper.searchByConsumerIdArtificerId(consumerId,artificerId);
            resultDTO.setData(list);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("系统异常，异常信息为：{}", e.getMessage());
            resultDTO.setMsg("系统异常");
            resultDTO.setStatus("-1");
        }
        return resultDTO;
    }

    @Override
    public ResponseResultDTO deleteCollection(Integer consumerId, Integer artificerId) {
        ResponseResultDTO resultDTO = new ResponseResultDTO();
        try {
            consumerCollectionMapper.deleteCollection(consumerId,artificerId);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("系统异常，异常信息为：{}", e.getMessage());
            resultDTO.setMsg("系统异常");
            resultDTO.setStatus("-1");
        }
        return resultDTO;
    }

    @Override
    public ResponseResultDTO isCollection(Integer artificerId, Integer customerId, Integer status) {
        ResponseResultDTO resultDTO = new ResponseResultDTO();
        try {
            if(artificerId==null||customerId==null||status==null){
                resultDTO.setMsg("PARAM IS NULL");
                resultDTO.setStatus("-1");
                return resultDTO;
            }
            ConsumerCollection consumerCollection=new ConsumerCollection();
            consumerCollection.setArtificerId(artificerId);
            consumerCollection.setCustomerId(customerId);
            ConsumerCollection collection = consumerCollectionMapper.selectOne(consumerCollection);
            if(collection==null){
                consumerCollection.setStatus(status);
                consumerCollection.setCollectionTime(new Date());
                consumerCollectionMapper.insert(consumerCollection);
            }else{
                consumerCollectionMapper.saveCollection(artificerId,customerId,status);
            }

        } catch (Exception e) {
            e.printStackTrace();
            log.info("系统异常，异常信息为：{}", e);
            resultDTO.setMsg("系统异常");
            resultDTO.setStatus("-1");
        }
        return resultDTO;
    }
}
